﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QASampling.BusinessObject;
using QASampling.QAControl;

namespace QASampling.UI
{
    public partial class frm1111DM : BaseForm
    {
        private static string PAGE_1111_A_DM_TITLE = "Formulir 1111 A DM ";
        private static string PAGE_1111_R_DM_TITLE = "Formulir 1111 R DM ";

        private static string ORI = "_ori";

        private static string COL_DOC_ID = "DocId";
        private static string COL_NAMA_PEMBELI = "NamaPembeli";
        private static string COL_NPWP = "NPWP";
        private static string COL_DPP = "DPP";
        private static string COL_PPN = "PPN";
        private static string COL_PPNBM = "PPnBM";
        private static string COL_ID = "ID";

        private static string COL_DOC_ID_ORI = "DocId" + ORI;
        private static string COL_NAMA_PEMBELI_ORI = "NamaPembeli" + ORI;
        private static string COL_NPWP_ORI = "NPWP" + ORI;
        private static string COL_DPP_ORI = "DPP" + ORI;
        private static string COL_PPN_ORI = "PPN" + ORI;
        private static string COL_PPNBM_ORI = "PPnBM" + ORI;
        
        private Form1111DM form;

        public frm1111DM(string docId) : this()
        {
            this.form = DAO.GetForm1111DM(docId);
            if (this.form == null)
                throw new Exception("Dokumen dengan DocId: " + docId + " tidak ditemukan");


            txtNamaPTKP.Text = form.NamaPtkp;
            txtAlamat.Text = form.Alamat;
            txtNPWP.Text = form.Npwp;
            txtMasa.Text = form.MasaPajak;

            txt_Page1_I_A.Text = form.I_A;
            txt_Page1_I_A.OriginalValue = form.I_A;
            txt_Page1_I_B.Text = form.I_B;
            txt_Page1_I_B.OriginalValue = form.I_B;
            txt_Page1_I_C.Text = form.I_C;
            txt_Page1_I_C.OriginalValue = form.I_C;

            txt_Page1_II_A_1.Text = form.II_A_1;
            txt_Page1_II_A_1.OriginalValue = form.II_A_1;
            txt_Page1_II_A_2.Text = form.II_A_2;
            txt_Page1_II_A_2.OriginalValue = form.II_A_2;
            txt_Page1_II_A_3.Text = form.II_A_3;
            txt_Page1_II_A_3.OriginalValue = form.II_A_3;

            txt_Page1_II_B.Text = form.II_B;
            txt_Page1_II_B.OriginalValue = form.II_B;

            txt_Page1_II_C_1_PRCN.Text = form.II_C_1_PRCN;
            txt_Page1_II_C_1.Text = form.II_C_1;
            txt_Page1_II_C_1.OriginalValue = form.II_C_1;
            txt_Page1_II_C_2_PRCN.Text = form.II_C_2_PRCN;
            txt_Page1_II_C_2.Text = form.II_C_2;
            txt_Page1_II_C_2.OriginalValue = form.II_C_2;
            txt_Page1_II_C_3.Text = form.II_C_3;
            txt_Page1_II_C_3.OriginalValue = form.II_C_3;

            txt_Page1_II_D_1.Text = form.II_D_1;
            txt_Page1_II_D_1.OriginalValue = form.II_D_1;
            txt_Page1_II_D_2_TGL.Text = form.II_D_2_TGL;
            txt_Page1_II_D_2.Text = form.II_D_2;
            txt_Page1_II_D_2.OriginalValue = form.II_D_2;
            txt_Page1_II_D_3.Text = form.II_D_3;
            txt_Page1_II_D_3.OriginalValue = form.II_D_3;

            txt_Page1_II_E.Text = form.II_E;
            txt_Page1_II_E.OriginalValue = form.II_E;

            txt_Page1_II_F.Text = form.II_F;
            txt_Page1_II_F.OriginalValue = form.II_F;

            txt_Page1_II_G.Text = form.II_G;
            txt_Page1_II_G.OriginalValue = form.II_G;

            txt_Page1_III_A.Text = form.III_A;
            txt_Page1_III_A.OriginalValue = form.III_A;
            txt_Page1_III_B.Text = form.III_B;
            txt_Page1_III_B.OriginalValue = form.III_B;

            txt_Page1_IV_A.Text = form.IV_A;
            txt_Page1_IV_A.OriginalValue = form.IV_A;
            txt_Page1_IV_B.Text = form.IV_B;
            txt_Page1_IV_B.OriginalValue = form.IV_B;
            txt_Page1_IV_C.Text = form.IV_C;
            txt_Page1_IV_C.OriginalValue = form.IV_C;
            txt_Page1_IV_D.Text = form.IV_D;
            txt_Page1_IV_D.OriginalValue = form.IV_D;
            txt_Page1_IV_E.Text = form.IV_E;
            txt_Page1_IV_E.OriginalValue = form.IV_E;

            foreach (Form1111DM_Item i in form.A_Page.Items)
            {
                string[] values = { i.DocId, i.Nama, i.Npwp, i.Dpp, i.Ppn, i.PPnBM,
                                       i.Nama, i.Npwp, i.Dpp, i.Ppn, i.PPnBM, i.Id };
                dgv_1111_A_DM.Rows.Add(values);
            }

            txtTotalDPP_A_DM.Text = form.A_Page.TotalDpp;
            txtTotalDPP_A_DM.OriginalValue = form.A_Page.TotalDpp;

            txtTotalPPN_A_DM.Text = form.A_Page.TotalPpn;
            txtTotalPPN_A_DM.OriginalValue = form.A_Page.TotalPpn;

            txtTotalPPnBM_A_DM.Text = form.A_Page.TotalPPnBM;
            txtTotalPPnBM_A_DM.OriginalValue = form.A_Page.TotalPPnBM;



             foreach (Form1111DM_Item i in form.R_Page.Items)
             {
                 string[] values = { i.DocId, i.Nama, i.Npwp, i.Dpp, i.Ppn, i.PPnBM,
                                       i.Nama, i.Npwp, i.Dpp, i.Ppn, i.PPnBM, i.Id };
                 dgv_1111_R_DM.Rows.Add(values);
             }

             txtTotalDPP_R_DM.Text = form.R_Page.TotalDpp;
             txtTotalDPP_R_DM.OriginalValue = form.R_Page.TotalDpp;

             txtTotalPPN_R_DM.Text = form.R_Page.TotalPpn;
             txtTotalPPN_R_DM.OriginalValue = form.R_Page.TotalPpn;

             txtTotalPPnBM_R_DM.Text = form.R_Page.TotalPPnBM;
             txtTotalPPnBM_R_DM.OriginalValue = form.R_Page.TotalPPnBM;
        }

        public frm1111DM()
        {
            InitializeComponent();
            PopulateDataGridColumns();
        }

        private void PopulateDataGridColumns()
        {
            int idx;

            idx = dgv_1111_A_DM.Columns.Add(COL_DOC_ID, "Doc Id");
            dgv_1111_A_DM.Columns[idx].Visible = false;
            dgv_1111_A_DM.Columns.Add(COL_NAMA_PEMBELI, "Nama Pembeli");
            dgv_1111_A_DM.Columns.Add(COL_NPWP, "NPWP");
            dgv_1111_A_DM.Columns.Add(COL_DPP, "DPP");
            dgv_1111_A_DM.Columns.Add(COL_PPN, "PPN");
            dgv_1111_A_DM.Columns.Add(COL_PPNBM, "PPnBM");

            idx = dgv_1111_A_DM.Columns.Add(COL_NAMA_PEMBELI_ORI, "Nama Pembeli");
            dgv_1111_A_DM.Columns[idx].Visible = false;
            idx = dgv_1111_A_DM.Columns.Add(COL_NPWP_ORI, "NPWP");
            dgv_1111_A_DM.Columns[idx].Visible = false;
            idx = dgv_1111_A_DM.Columns.Add(COL_DPP_ORI, "DPP");
            dgv_1111_A_DM.Columns[idx].Visible = false;
            idx = dgv_1111_A_DM.Columns.Add(COL_PPN_ORI, "PPN");
            dgv_1111_A_DM.Columns[idx].Visible = false;
            idx = dgv_1111_A_DM.Columns.Add(COL_PPNBM_ORI, "PPnBM");
            dgv_1111_A_DM.Columns[idx].Visible = false;
            idx = dgv_1111_A_DM.Columns.Add(COL_ID, "Id");
            dgv_1111_A_DM.Columns[idx].Visible = false;



            idx = dgv_1111_R_DM.Columns.Add(COL_DOC_ID, "Doc Id");
            dgv_1111_R_DM.Columns[idx].Visible = false;
            dgv_1111_R_DM.Columns.Add(COL_NAMA_PEMBELI, "Nama Pembeli");
            dgv_1111_R_DM.Columns.Add(COL_NPWP, "NPWP");
            dgv_1111_R_DM.Columns.Add(COL_DPP, "DPP");
            dgv_1111_R_DM.Columns.Add(COL_PPN, "PPN");
            dgv_1111_R_DM.Columns.Add(COL_PPNBM, "PPnBM");

            idx = dgv_1111_R_DM.Columns.Add(COL_NAMA_PEMBELI_ORI, "Nama Pembeli");
            dgv_1111_R_DM.Columns[idx].Visible = false;
            idx = dgv_1111_R_DM.Columns.Add(COL_NPWP_ORI, "NPWP");
            dgv_1111_R_DM.Columns[idx].Visible = false;
            idx = dgv_1111_R_DM.Columns.Add(COL_DPP_ORI, "DPP");
            dgv_1111_R_DM.Columns[idx].Visible = false;
            idx = dgv_1111_R_DM.Columns.Add(COL_PPN_ORI, "PPN");
            dgv_1111_R_DM.Columns[idx].Visible = false;
            idx = dgv_1111_R_DM.Columns.Add(COL_PPNBM_ORI, "PPnBM");
            dgv_1111_R_DM.Columns[idx].Visible = false;
            idx = dgv_1111_R_DM.Columns.Add(COL_ID, "Id");
            dgv_1111_R_DM.Columns[idx].Visible = false;
        }

        private void dgv_1111_A_DM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_1111_A_DM.SelectedRows.Count == 0) return;

            DataGridViewRow row = dgv_1111_A_DM.SelectedRows[0];
            frm1111DM_A_R frm = new frm1111DM_A_R();
            frm.txtDocId.Text = row.Cells[COL_DOC_ID].Value.ToString();
            frm.txtNama.Text = row.Cells[COL_NAMA_PEMBELI].Value.ToString();
            frm.txtNPWP.Text = row.Cells[COL_NPWP].Value.ToString();
            frm.txtDpp.Text = row.Cells[COL_DPP].Value.ToString();
            frm.txtPpn.Text = row.Cells[COL_PPN].Value.ToString();
            frm.txtPPnBM.Text = row.Cells[COL_PPNBM].Value.ToString();

            frm.txtDpp.OriginalValue = row.Cells[COL_DPP_ORI].Value.ToString();
            frm.txtPpn.OriginalValue = row.Cells[COL_PPN_ORI].Value.ToString();
            frm.txtPPnBM.OriginalValue = row.Cells[COL_PPNBM_ORI].Value.ToString();

            frm.Text = PAGE_1111_A_DM_TITLE;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                row.Cells[COL_DPP].Value = frm.txtDpp.Text;
                row.Cells[COL_PPN].Value = frm.txtPpn.Text;
                row.Cells[COL_PPNBM].Value = frm.txtPPnBM.Text;

                row.Cells[COL_DPP_ORI].Value = frm.txtDpp.OriginalValue;
                row.Cells[COL_PPN_ORI].Value = frm.txtPpn.OriginalValue;
                row.Cells[COL_PPNBM_ORI].Value = frm.txtPPnBM.OriginalValue;

                SetDataGridBackgroundColor(dgv_1111_A_DM);
            }
        }

        private void dgv_1111_R_DM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_1111_R_DM.SelectedRows.Count == 0) return;

            DataGridViewRow row = dgv_1111_R_DM.SelectedRows[0];
            frm1111DM_A_R frm = new frm1111DM_A_R();
            frm.txtDocId.Text = row.Cells[COL_DOC_ID].Value.ToString();
            frm.txtNama.Text = row.Cells[COL_NAMA_PEMBELI].Value.ToString();
            frm.txtNPWP.Text = row.Cells[COL_NPWP].Value.ToString();
            frm.txtDpp.Text = row.Cells[COL_DPP].Value.ToString();
            frm.txtPpn.Text = row.Cells[COL_PPN].Value.ToString();
            frm.txtPPnBM.Text = row.Cells[COL_PPNBM].Value.ToString();

            frm.txtDpp.OriginalValue = row.Cells[COL_DPP_ORI].Value.ToString();
            frm.txtPpn.OriginalValue = row.Cells[COL_PPN_ORI].Value.ToString();
            frm.txtPPnBM.OriginalValue = row.Cells[COL_PPNBM_ORI].Value.ToString();

            frm.Text = PAGE_1111_R_DM_TITLE;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                row.Cells[COL_DPP].Value = frm.txtDpp.Text;
                row.Cells[COL_PPN].Value = frm.txtPpn.Text;
                row.Cells[COL_PPNBM].Value = frm.txtPPnBM.Text;

                row.Cells[COL_DPP_ORI].Value = frm.txtDpp.OriginalValue;
                row.Cells[COL_PPN_ORI].Value = frm.txtPpn.OriginalValue;
                row.Cells[COL_PPNBM_ORI].Value = frm.txtPPnBM.OriginalValue;

                SetDataGridBackgroundColor(dgv_1111_R_DM);
            }
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            string msg = "Anda yakin akan membatalkan proses koreksi?";
            if (MessageBox.Show(msg, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                this.Close();
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            string msg = "Anda yakin akan menyimpan koreksi yang telah Anda lakukan?";
            if (MessageBox.Show(msg, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                if(ValidateForm())
                    SaveData();
            }
        }

        private void RefreshLabel()
        {
            label_IA_P_Brg.Visible = false;
            label_IB_P_Jasa.Visible = false;
            label_IC_Jumlah.Visible = false;
            label_IIA_P_Brg.Visible = false;
            label_IIA_P_Jasa.Visible = false;
            label_IIA_Jumlah.Visible = false;
            label_IIB_PPN.Visible = false;
            label_IIC_P_Brg.Visible = false;
            label_IIC_P_Jasa.Visible = false;
            label_IIC_Jumlah.Visible = false;
            label_IID_PPN.Visible = false;
            label_IID_PPN_Ms_Pajak.Visible = false;
            label_IID_Jumlah.Visible = false;
            label_IIE_PPN.Visible = false;
            label_IIF_PPN.Visible = false;
            label_IIG_PPN.Visible = false;
            label_IIIA_Jumlah.Visible = false;
            label_IIIB_PPN.Visible = false;
            label_IVA_PPNBM.Visible = false;
            label_IVB_PPNBM.Visible = false;
            label_IVC_PPNBM.Visible = false;
            label_IVD_PPNBM.Visible = false;
            label_IVE_PPNBM.Visible = false;
        }

        private bool ValidateForm()
        {
            RefreshLabel();
            bool a = true;
            if (String.IsNullOrEmpty(txt_Page1_I_A.Text))
            {
                label_IA_P_Brg.Visible = true;
                label_IA_P_Brg.Text = "Penyerahan Barang tidak boleh kosong";
                txt_Page1_I_A.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_I_B.Text))
            {
                label_IB_P_Jasa.Visible = true;
                label_IB_P_Jasa.Text = "Penyerahan Jasa tidak boleh kosong";
                txt_Page1_I_B.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_I_C.Text))
            {
                label_IC_Jumlah.Visible = true;
                label_IC_Jumlah.Text = "Jumlah (I.A + I.B) tidak boleh kosong";
                txt_Page1_I_C.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_I_C.Text) && !String.IsNullOrEmpty(txt_Page1_I_A.Text) && !String.IsNullOrEmpty(txt_Page1_I_B.Text) &&
                long.Parse(txt_Page1_I_C.Text) != long.Parse(txt_Page1_I_A.Text) + long.Parse(txt_Page1_I_B.Text))
            {
                label_IC_Jumlah.Visible = true;
                label_IC_Jumlah.Text = "Jumlah (I.A + I.B) salah";
                txt_Page1_I_C.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_I_C.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_A_1.Text))
            {
                label_IIA_P_Brg.Visible = true;
                label_IIA_P_Brg.Text = "Penyerahan Barang tidak boleh kosong";
                txt_Page1_II_A_1.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_A_1.Text) && !String.IsNullOrEmpty(txt_Page1_I_A.Text) &&
                long.Parse(txt_Page1_II_A_1.Text) != 0.1 * long.Parse(txt_Page1_I_A.Text))
            {
                label_IIA_P_Brg.Visible = true;
                label_IIA_P_Brg.Text = "Penyerahan Barang salah";
                txt_Page1_II_A_1.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_A_1.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_A_2.Text))
            {
                label_IIA_P_Jasa.Visible = true;
                label_IIA_P_Jasa.Text = "Penyerahan Jasa tidak boleh kosong";
                txt_Page1_II_A_2.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_A_2.Text) && !String.IsNullOrEmpty(txt_Page1_I_B.Text) &&
                long.Parse(txt_Page1_II_A_2.Text) != 0.1 * long.Parse(txt_Page1_I_B.Text))
            {
                label_IIA_P_Jasa.Visible = true;
                label_IIA_P_Jasa.Text = "Penyerahan Jasa salah";
                txt_Page1_II_A_2.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_A_2.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_A_3.Text))
            {
                label_IIA_Jumlah.Visible = true;
                label_IIA_Jumlah.Text = "Jumlah (II.A.1 + II.A.2) tidak boleh kosong";
                txt_Page1_II_A_3.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_A_3.Text) && !String.IsNullOrEmpty(txt_Page1_II_A_1.Text) && !String.IsNullOrEmpty(txt_Page1_II_A_2.Text) &&
                long.Parse(txt_Page1_II_A_3.Text) != long.Parse(txt_Page1_II_A_1.Text) + long.Parse(txt_Page1_II_A_2.Text))
            {
                label_IIA_Jumlah.Visible = true;
                label_IIA_Jumlah.Text = "Jumlah (II.A.1 + II.A.2) salah";
                txt_Page1_II_A_3.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_A_3.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_B.Text))
            {
                label_IIB_PPN.Visible = true;
                label_IIB_PPN.Text = "PPN disetor di muka dalam Masa Pajak yang sama tidak boleh kosong";
                txt_Page1_II_B.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_II_C_1.Text))
            {
                label_IIC_P_Brg.Visible = true;
                label_IIC_P_Brg.Text = "Penyerahan Barang tidak boleh kosong";
                txt_Page1_II_C_1.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_C_1.Text) && !String.IsNullOrEmpty(txt_Page1_II_A_1.Text) && !String.IsNullOrEmpty(txt_Page1_II_C_1_PRCN.Text) &&
               long.Parse(txt_Page1_II_C_1.Text) != long.Parse(txt_Page1_II_A_1.Text) * long.Parse(txt_Page1_II_C_1_PRCN.Text) / 100)
            {
                label_IIC_P_Brg.Visible = true;
                label_IIC_P_Brg.Text = "Penyerahan Barang salah";
                txt_Page1_II_C_1.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_C_1.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_C_2.Text))
            {
                label_IIC_P_Jasa.Visible = true;
                label_IIC_P_Jasa.Text = "Penyerahan Jasa tidak boleh kosong";
                txt_Page1_II_C_2.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_C_2.Text) && !String.IsNullOrEmpty(txt_Page1_II_A_2.Text) && !String.IsNullOrEmpty(txt_Page1_II_C_2_PRCN.Text) &&
               long.Parse(txt_Page1_II_C_2.Text) != long.Parse(txt_Page1_II_A_2.Text) * long.Parse(txt_Page1_II_C_2_PRCN.Text) / 100)
            {
                label_IIC_P_Jasa.Visible = true;
                label_IIC_P_Jasa.Text = "Penyerahan Jasa salah";
                txt_Page1_II_C_2.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_C_2.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_C_3.Text))
            {
                label_IIC_Jumlah.Visible = true;
                label_IIC_Jumlah.Text = "Jumlah (II.C.1 + II.C.2) tidak boleh kosong";
                txt_Page1_II_C_3.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_C_3.Text) && !String.IsNullOrEmpty(txt_Page1_II_C_1.Text) && !String.IsNullOrEmpty(txt_Page1_II_C_2.Text) &&
                long.Parse(txt_Page1_II_C_3.Text) != long.Parse(txt_Page1_II_C_1.Text) + long.Parse(txt_Page1_II_C_2.Text))
            {
                label_IIC_Jumlah.Visible = true;
                label_IIC_Jumlah.Text = "Jumlah (II.C.1 + II.C.2) salah";
                txt_Page1_II_C_3.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_C_3.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_D_1.Text))
            {
                label_IID_PPN.Visible = true;
                label_IID_PPN.Text = "Kompensasi kelebihan PPN Masa Pajak sebelumnya tidak boleh kosong";
                txt_Page1_II_D_1.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_II_D_2.Text))
            {
                label_IID_PPN_Ms_Pajak.Visible = true;
                label_IID_PPN_Ms_Pajak.Text = "Kompensasi kelebihan PPN karena pembetulan SPT PPN Masa Pajak tidak boleh kosong";
                txt_Page1_II_D_2.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_II_D_3.Text))
            {
                label_IID_Jumlah.Visible = true;
                label_IID_Jumlah.Text = "Jumlah (II.D.1 + II.D.2) tidak boleh kosong";
                txt_Page1_II_D_3.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_D_3.Text) && !String.IsNullOrEmpty(txt_Page1_II_D_1.Text) && !String.IsNullOrEmpty(txt_Page1_II_D_2.Text) &&
                long.Parse(txt_Page1_II_D_3.Text) != long.Parse(txt_Page1_II_D_1.Text) + long.Parse(txt_Page1_II_D_2.Text))
            {
                label_IID_Jumlah.Visible = true;
                label_IID_Jumlah.Text = "Jumlah (II.D.1 + II.D.2) salah";
                txt_Page1_II_D_3.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_D_3.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_E.Text))
            {
                label_IIE_PPN.Visible = true;
                label_IIE_PPN.Text = "PPN kurang atau (lebih) bayar (II.A.3 - II.B - II.C.3 - II.D.3) tidak boleh kosong";
                txt_Page1_II_E.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_E.Text) && !String.IsNullOrEmpty(txt_Page1_II_A_3.Text) && !String.IsNullOrEmpty(txt_Page1_II_B.Text) &&
                !String.IsNullOrEmpty(txt_Page1_II_C_3.Text) && !String.IsNullOrEmpty(txt_Page1_II_D_3.Text) &&
                long.Parse(txt_Page1_II_E.Text) != long.Parse(txt_Page1_II_A_3.Text) - long.Parse(txt_Page1_II_B.Text)
                - long.Parse(txt_Page1_II_C_3.Text) - long.Parse(txt_Page1_II_D_3.Text))
            {
                label_IIE_PPN.Visible = true;
                label_IIE_PPN.Text = "PPN kurang atau (lebih) bayar (II.A.3 - II.B - II.C.3 - II.D.3) salah";
                txt_Page1_II_E.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_E.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_II_F.Text))
            {
                label_IIF_PPN.Visible = true;
                label_IIF_PPN.Text = "PPN kurang atau (lebih) bayar pada SPT yang dibetulkan tidak boleh kosong";
                txt_Page1_II_F.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_II_G.Text))
            {
                label_IIG_PPN.Visible = true;
                label_IIG_PPN.Text = "PPN kurang atau (lebih) bayar pada SPT karena pembetulan (II.E - II.F) tidak boleh kosong";
                txt_Page1_II_G.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_II_G.Text) && !String.IsNullOrEmpty(txt_Page1_II_E.Text) && !String.IsNullOrEmpty(txt_Page1_II_F.Text) &&
                long.Parse(txt_Page1_II_G.Text) != long.Parse(txt_Page1_II_E.Text) - long.Parse(txt_Page1_II_F.Text))
            {
                label_IIG_PPN.Visible = true;
                label_IIG_PPN.Text = "PPN kurang atau (lebih) bayar pada SPT karena pembetulan (II.E - II.F) salah";
                txt_Page1_II_G.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_II_G.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_III_A.Text))
            {
                label_IIIA_Jumlah.Visible = true;
                label_IIIA_Jumlah.Text = "Jumlah Dasar Pengenaan Pajak tidak boleh kosong";
                txt_Page1_III_A.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_III_B.Text))
            {
                label_IIIB_PPN.Visible = true;
                label_IIIB_PPN.Text = "PPN Terutang tidak boleh kosong";
                txt_Page1_III_B.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_III_B.Text) && !String.IsNullOrEmpty(txt_Page1_III_A.Text) &&
               long.Parse(txt_Page1_III_B.Text) != 0.1 * long.Parse(txt_Page1_III_A.Text))
            {
                label_IIIB_PPN.Visible = true;
                label_IIIB_PPN.Text = "PPN Terutang salah";
                txt_Page1_III_B.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_III_B.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_IV_A.Text))
            {
                label_IVA_PPNBM.Visible = true;
                label_IVA_PPNBM.Text = "PPnBM yang harus dipungut sendiri tidak boleh kosong";
                txt_Page1_IV_A.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_IV_B.Text))
            {
                label_IVB_PPNBM.Visible = true;
                label_IVB_PPNBM.Text = "PPnBM disetor dimuka dalam Masa Pajak yang sama tidak boleh kosong";
                txt_Page1_IV_B.BackColor = Color.Red;
                a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_IV_C.Text))
            {
                label_IVC_PPNBM.Visible = true;
                label_IVC_PPNBM.Text = "PPnBM kurang atau (lebih) bayar (IV.A - IV.B) tidak boleh kosong";
                txt_Page1_IV_C.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_IV_C.Text) && !String.IsNullOrEmpty(txt_Page1_IV_A.Text) && !String.IsNullOrEmpty(txt_Page1_IV_B.Text) &&
                long.Parse(txt_Page1_IV_C.Text) != long.Parse(txt_Page1_IV_A.Text) - long.Parse(txt_Page1_IV_B.Text))
            {
                label_IVC_PPNBM.Visible = true;
                label_IVC_PPNBM.Text = "PPnBM kurang atau (lebih) bayar (IV.A - IV.B) salah";
                txt_Page1_IV_C.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_IV_C.BackColor = Color.White;

            if (String.IsNullOrEmpty(txt_Page1_IV_D.Text))
            {
                label_IVD_PPNBM.Visible = true;
                label_IVD_PPNBM.Text = "PPnBM kurang atau (lebih) bayar pada SPT yang dibetulkan tidak boleh kosong";
                txt_Page1_IV_D.BackColor = Color.Red;
                 a = false;
            }
            if (String.IsNullOrEmpty(txt_Page1_IV_E.Text))
            {
                label_IVE_PPNBM.Visible = true;
                label_IVE_PPNBM.Text = "PPnBM kurang atau (lebih) bayar karena pembetulan (IV.C - IV.D) tidak boleh kosong";
                txt_Page1_IV_E.BackColor = Color.Red;
                a = false;
            }
            else if (!String.IsNullOrEmpty(txt_Page1_IV_E.Text) && !String.IsNullOrEmpty(txt_Page1_IV_C.Text) && !String.IsNullOrEmpty(txt_Page1_IV_D.Text) &&
               long.Parse(txt_Page1_IV_E.Text) != long.Parse(txt_Page1_IV_C.Text) - long.Parse(txt_Page1_IV_D.Text))
            {
                label_IVE_PPNBM.Visible = true;
                label_IVE_PPNBM.Text = "PPnBM kurang atau (lebih) bayar karena pembetulan (IV.C - IV.D) salah";
                txt_Page1_IV_E.BackColor = Color.Red;
                a = false;
            }
            else
                txt_Page1_IV_E.BackColor = Color.White;

            long dpp = 0;
            long ppn = 0;
            long ppnbm = 0;
            long dpp1 = 0;
            long ppn1 = 0;
            long ppnbm1 = 0;
           
            for(int i = 0;i<=dgv_1111_A_DM.RowCount - 1;i++)
            {
                dpp += String.IsNullOrEmpty(dgv_1111_A_DM.Rows[i].Cells[3].Value.ToString()) ? 0 : long.Parse(dgv_1111_A_DM.Rows[i].Cells[3].Value.ToString());
                ppn += String.IsNullOrEmpty(dgv_1111_A_DM.Rows[i].Cells[4].Value.ToString()) ? 0 : long.Parse(dgv_1111_A_DM.Rows[i].Cells[4].Value.ToString());
                ppnbm += String.IsNullOrEmpty(dgv_1111_A_DM.Rows[i].Cells[5].Value.ToString()) ? 0 : long.Parse(dgv_1111_A_DM.Rows[i].Cells[5].Value.ToString());
            }

            if (String.IsNullOrEmpty(txtTotalDPP_A_DM.Text) || long.Parse(txtTotalDPP_A_DM.Text) != dpp)
            {
                txtTotalDPP_A_DM.BackColor = Color.Red;
                a = false;
            }
            else
                txtTotalDPP_A_DM.BackColor = Color.White;

            if (String.IsNullOrEmpty(txtTotalPPN_A_DM.Text) || long.Parse(txtTotalPPN_A_DM.Text) != ppn)
            {
                txtTotalPPN_A_DM.BackColor = Color.Red;
                a = false;
            }
            else
                txtTotalPPN_A_DM.BackColor = Color.White;

            if (String.IsNullOrEmpty(txtTotalPPnBM_A_DM.Text) || long.Parse(txtTotalPPnBM_A_DM.Text) != ppnbm)
            {
                txtTotalPPnBM_A_DM.BackColor = Color.Red;
                a = false;
            }
            else
                txtTotalPPnBM_A_DM.BackColor = Color.White;

            for(int i = 0;i<=dgv_1111_R_DM.RowCount - 1;i++)
            {
                dpp1 += String.IsNullOrEmpty(dgv_1111_R_DM.Rows[i].Cells[3].Value.ToString()) ? 0 : long.Parse(dgv_1111_R_DM.Rows[i].Cells[3].Value.ToString());
                ppn1 += String.IsNullOrEmpty(dgv_1111_R_DM.Rows[i].Cells[4].Value.ToString()) ? 0 : long.Parse(dgv_1111_R_DM.Rows[i].Cells[4].Value.ToString());
                ppnbm1 += String.IsNullOrEmpty(dgv_1111_R_DM.Rows[i].Cells[5].Value.ToString()) ? 0 : long.Parse(dgv_1111_R_DM.Rows[i].Cells[5].Value.ToString());
            }

            if (String.IsNullOrEmpty(txtTotalDPP_R_DM.Text) || long.Parse(txtTotalDPP_R_DM.Text) != dpp1)
            {
                txtTotalDPP_R_DM.BackColor = Color.Red;
                a = false;
            }
            else
                txtTotalDPP_R_DM.BackColor = Color.White;

            if (String.IsNullOrEmpty(txtTotalPPN_R_DM.Text) || long.Parse(txtTotalPPN_R_DM.Text) != ppn1)
            {
                txtTotalPPN_R_DM.BackColor = Color.Red;
                a = false;
            }
            else
                txtTotalPPN_R_DM.BackColor = Color.White;

            if (String.IsNullOrEmpty(txtTotalPPnBM_R_DM.Text) || long.Parse(txtTotalPPnBM_R_DM.Text) != ppnbm1)
            {
                txtTotalPPnBM_R_DM.BackColor = Color.Red;
                a = false;
            }
            else
                txtTotalPPnBM_R_DM.BackColor = Color.White;

            return a;
        }

        private void SaveData()
        {
            String log = GenerateChanegLog();

            form.I_A = txt_Page1_I_A.Text;
            form.I_B = txt_Page1_I_B.Text;
            form.I_C = txt_Page1_I_C.Text;
            form.II_A_1 = txt_Page1_II_A_1.Text;
            form.II_A_2 = txt_Page1_II_A_2.Text;
            form.II_A_3 = txt_Page1_II_A_3.Text;
            form.II_B = txt_Page1_II_B.Text;
            form.II_C_1 = txt_Page1_II_C_1.Text;
            form.II_C_2 = txt_Page1_II_C_2.Text;
            form.II_C_3 = txt_Page1_II_C_3.Text;
            form.II_D_1 = txt_Page1_II_D_1.Text;
            form.II_D_2 = txt_Page1_II_D_2.Text;
            form.II_D_3 = txt_Page1_II_D_3.Text;
            form.II_E = txt_Page1_II_E.Text;
            form.II_F = txt_Page1_II_F.Text;
            form.II_G = txt_Page1_II_G.Text;
            form.III_A = txt_Page1_III_A.Text;
            form.III_B = txt_Page1_III_B.Text;
            form.IV_A = txt_Page1_IV_A.Text;
            form.IV_B = txt_Page1_IV_B.Text;
            form.IV_C = txt_Page1_IV_C.Text;
            form.IV_D = txt_Page1_IV_D.Text;
            form.IV_E = txt_Page1_IV_E.Text;

            form.A_Page.TotalDpp = txtTotalDPP_A_DM.Text;
            form.A_Page.TotalPpn = txtTotalPPN_A_DM.Text;
            form.A_Page.TotalPPnBM = txtTotalPPnBM_A_DM.Text;
            form.R_Page.TotalDpp = txtTotalDPP_R_DM.Text;
            form.R_Page.TotalPpn = txtTotalPPN_R_DM.Text;
            form.R_Page.TotalPPnBM = txtTotalPPnBM_R_DM.Text;

            foreach (Form1111DM_Item i in form.A_Page.Items)
            {
                DataGridViewRow r = GetDataGridViewRowByIdAndDocId(i.Id, i.DocId, dgv_1111_A_DM);
                if (r != null)
                {
                    i.Dpp = r.Cells[3].Value.ToString();
                    i.Ppn = r.Cells[4].Value.ToString();
                    i.PPnBM = r.Cells[5].Value.ToString();
                }
            }

            foreach (Form1111DM_Item i in form.R_Page.Items)
            {
                DataGridViewRow r = GetDataGridViewRowByIdAndDocId(i.Id, i.DocId, dgv_1111_R_DM);
                if (r != null)
                {
                    i.Dpp = r.Cells[3].Value.ToString();
                    i.Ppn = r.Cells[4].Value.ToString();
                    i.PPnBM = r.Cells[5].Value.ToString();
                }
            }

            QAController.UpdateForm1111DM(this.form, log);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private DataGridViewRow GetDataGridViewRowByIdAndDocId(string Id, string docId, QAControl.QADataGrid dataGrid)
        {
            foreach (DataGridViewRow r in dataGrid.Rows)
                if (r.Cells[11].Value.Equals(Id) && r.Cells[0].Value.Equals(docId))
                    return r;
            return null;
        }

        private string GenerateChanegLog()
        {
            StringBuilder sbLog = new StringBuilder();
            foreach (Control c in tbp1111_DM.Controls)
            {
                QATextbox qt = c as QATextbox;
                if (qt != null && qt.Text != qt.OriginalValue)
                    sbLog.AppendLine(qt.Name.Replace("txt_Page1_", "") + ": " + qt.OriginalValue + " -> " + qt.Text);
            }

            foreach (DataGridViewRow r in dgv_1111_A_DM.Rows)
            {
                string dppCurrent = r.Cells[COL_DPP].Value.ToString();
                string dppOri = r.Cells[COL_DPP_ORI].Value.ToString();
                if (dppCurrent != dppOri)
                    sbLog.AppendLine("DPP_A milik " + r.Cells[COL_NAMA_PEMBELI].Value + ": " + dppOri + " -> " + dppCurrent);

                string ppnCurrent = r.Cells[COL_PPN].Value.ToString();
                string ppnOri = r.Cells[COL_PPN_ORI].Value.ToString();
                if (ppnCurrent != ppnOri)
                    sbLog.AppendLine("PPN_A milik " + r.Cells[COL_NAMA_PEMBELI].Value + ": " + ppnOri + " -> " + ppnCurrent);

                string ppnbmCurrent = r.Cells[COL_PPNBM].Value.ToString();
                string ppnbmOri = r.Cells[COL_PPNBM_ORI].Value.ToString();
                if (ppnbmCurrent != ppnbmOri)
                    sbLog.AppendLine("PPnBM_A milik " + r.Cells[COL_NAMA_PEMBELI].Value + ": " + ppnbmOri + " -> " + ppnbmCurrent);
            }

            foreach (Control c in tbp1111_A_DM.Controls)
            {
                QATextbox qt = c as QATextbox;
                if (qt != null && qt.Text != qt.OriginalValue)
                    sbLog.AppendLine(qt.Name.Replace("txt", "").Replace("_DM", "") + ": " + qt.OriginalValue + " -> " + qt.Text);
            }


            foreach (DataGridViewRow r in dgv_1111_R_DM.Rows)
            {
                string dppCurrent = r.Cells[COL_DPP].Value.ToString();
                string dppOri = r.Cells[COL_DPP_ORI].Value.ToString();
                if (dppCurrent != dppOri)
                    sbLog.AppendLine("DPP_R milik " + r.Cells[COL_NAMA_PEMBELI].Value + ": " + dppOri + " -> " + dppCurrent);

                string ppnCurrent = r.Cells[COL_PPN].Value.ToString();
                string ppnOri = r.Cells[COL_PPN_ORI].Value.ToString();
                if (ppnCurrent != ppnOri)
                    sbLog.AppendLine("PPN_R milik " + r.Cells[COL_NAMA_PEMBELI].Value + ": " + ppnOri + " -> " + ppnCurrent);

                string ppnbmCurrent = r.Cells[COL_PPNBM].Value.ToString();
                string ppnbmOri = r.Cells[COL_PPNBM_ORI].Value.ToString();
                if (ppnbmCurrent != ppnbmOri)
                    sbLog.AppendLine("PPnBM_R milik " + r.Cells[COL_NAMA_PEMBELI].Value + ": " + ppnbmOri + " -> " + ppnbmCurrent);

            }

            foreach (Control c in tbp1111_R_DM.Controls)
            {
                QATextbox qt = c as QATextbox;
                if (qt != null && qt.Text != qt.OriginalValue)
                    sbLog.AppendLine(qt.Name.Replace("txt", "").Replace("_DM", "") + ": " + qt.OriginalValue + " -> " + qt.Text);
            }

            return sbLog.ToString();
        }

        private void SetDataGridBackgroundColor(QAControl.QABaseDataGrid dataGrid)
        {
            foreach (DataGridViewRow row in dataGrid.Rows)
            {
                int length = 5;
                for (int i = 1; i <= length; i++)
                {
                    if (!row.Cells[i].Value.Equals(row.Cells[i + length].Value))

                        row.Cells[i].Style.BackColor = Color.LightGreen;
                    else
                        row.Cells[i].Style.BackColor = Color.White;

                }
            }

            dataGrid.ClearSelection();
        }

    }
}
